package org.zjx.task;

import org.zjx.common.command.ICommand;
import org.zjx.common.command.battle.ExitCommand;
import org.zjx.common.entity.Quest;
import org.zjx.common.model.RouteProperties;
import org.zjx.task.tasks.CatchGhostsTask;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TaskManager {
    public static Map<String, List<Task>> taskMap = new ConcurrentHashMap<>();

    private static final class InstanceHolder {
        static final TaskManager instance = new TaskManager();
    }

    public static TaskManager getInstance() {
        return InstanceHolder.instance;
    }

    public ICommand getCommand(RouteProperties routeProperties) {
        if (!taskMap.containsKey(routeProperties.getPlayerId())) addTask(routeProperties.getPlayerId());
        Task task = taskMap.get(routeProperties.getPlayerId()).stream()
                .filter(t -> !t.isCompleted())
                .findFirst()
                .orElse(null);
        if (task == null) {
            //任务执行完成，退出游戏
            return new ExitCommand();
        }
        return task.getCommand(routeProperties);
    }

    private void addTask(String playerId) {
//        taskMap.put(playerId, List.of(new DivisionTask(sects), new CatchGhostsTask()));
        taskMap.put(playerId, Collections.singletonList(new CatchGhostsTask()));
    }

    public void complete(String playerId,Quest quest) {
        List<Task> tasks = taskMap.get(playerId);
        if (tasks == null || quest == null) return;
        for (Task task : tasks) {
            task.complete(quest);
        }
    }
}
